package com.hjf.ditubiaozhu.base.recycler.widget;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.FrameLayout;

import com.hjf.ditubiaozhu.R;
import com.hjf.ditubiaozhu.base.recycler.BaseSwipeAdapter;
import com.hjf.ditubiaozhu.base.recycler.OnInsideRefreshListener;
import com.hjf.ditubiaozhu.base.recycler.OnMyRefreshListener;


public class SwipeRecyclerView extends FrameLayout implements SwipeRefreshLayout.OnRefreshListener {
public static final String TAG="sWipeRecyclerView";
    private MySwipeRefreshLayout mSwipeRefreshLayout;
    private RecyclerView mRecyclerView;
    private LinearLayoutManager mLinearLayoutManager;
    private OnMyRefreshListener mOnMyRefreshListener;

    /**
     * 内部刷新监听 由base调用,传递到自定义布局刷新并触发外部自定义刷新接口
     */
    private OnInsideRefreshListener mOnInsideRefreshListener;
    private BaseSwipeAdapter mSwipeAdapter;
    private Context mContext;

    public SwipeRecyclerView(Context context) {
        this(context, null);
        mContext = context;
    }

    public SwipeRecyclerView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        mContext = context;
    }

    public SwipeRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        inflate(context, R.layout.swiperecyclerview, this);
        mSwipeRefreshLayout = (MySwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        init();
    }

    private void init() {

        mSwipeRefreshLayout.setColorSchemeResources(
                R.color.swiperefresh_color1,
                R.color.swiperefresh_color2,
                R.color.swiperefresh_color3,
                R.color.swiperefresh_color4);

        mSwipeRefreshLayout.setOnRefreshListener(this);
        mLinearLayoutManager = new LinearLayoutManager(getContext());
        mRecyclerView.setLayoutManager(mLinearLayoutManager);
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                //获取状态
                int loadStatus = mSwipeAdapter.getLoadStatus();
                //是否还有更多,还要加载?
                boolean hasMore = mSwipeAdapter.isHasMore();
                //最后一个全部可见item的位置==用户数据总数
                boolean isUserLast = mSwipeAdapter.userDataCount()==mLinearLayoutManager.findLastCompletelyVisibleItemPosition();
                Log.e(TAG,"loadStatus:"+loadStatus+";hasMore:"+hasMore+";userCount:"+mSwipeAdapter.userDataCount()+
                    ";lastPostion:"+mLinearLayoutManager.findLastCompletelyVisibleItemPosition());

                //如果上述条件全部满足
                if (loadStatus == 0 && hasMore && isUserLast) {
                    mSwipeAdapter.setLoadStatus(BaseSwipeAdapter.LOAD_STATUS_UP);// 下拉 追加
                    if (null != mOnMyRefreshListener) {
                        mOnMyRefreshListener.upRefresh(mSwipeAdapter.userDataCount());
                    }
                }
            }
        });
        //内部刷新监听 由base调用,传递到自定义布局刷新并触发调用接口
        mOnInsideRefreshListener = new OnInsideRefreshListener() {
            @Override
            public void downRefresh() {
                mSwipeRefreshLayout.setRefreshing(true);
                mOnMyRefreshListener.downRefresh();
            }

            @Override
            public void upRefresh(int count) {
                mOnMyRefreshListener.upRefresh(count);
            }

            @Override
            public void refreshComplete() {
                mSwipeRefreshLayout.setRefreshing(false);
            }
        };
    }



    @Override
    public void onRefresh() {
        mSwipeAdapter.setLoadStatus(BaseSwipeAdapter.LOAD_STATUS_DOWN);
        mOnMyRefreshListener.downRefresh();
    }

    /**
     * 刷新回调
     */
    public void setOnMyRefreshListener(OnMyRefreshListener onMyRefreshListener) {
        mOnMyRefreshListener = onMyRefreshListener;
    }
    /**
     * 开启下拉刷新
     */
    public void startDownRefresh() {
        mSwipeAdapter.setLoadStatus(BaseSwipeAdapter.LOAD_STATUS_DOWN);
        mSwipeRefreshLayout.setRefreshing(true);
        mRecyclerView.smoothScrollToPosition(0);
        mOnMyRefreshListener.downRefresh();
    }

    /**
     * 设置Adapter
     */
    public void setAdapter(BaseSwipeAdapter swipeAdapter) {
        mSwipeAdapter = swipeAdapter;
        mSwipeAdapter.setmOnRefreshListener(mOnInsideRefreshListener);
        mRecyclerView.setAdapter(mSwipeAdapter);
    }

    /**
     * 获取RecyclerView
     */
    public RecyclerView getRecyclerView() {
        return mRecyclerView;
    }

    /**
     * 页面异常状态
     *
     * @param state 加载状态-1:初始化;0:普通状态;1:下拉刷新;2:加载更多;3:加载出错
     */
    public void downRefreshComplete(int state) {
        mSwipeAdapter.setLoadStatus(state);
        mSwipeAdapter.notifyDataSetChanged();
        mOnInsideRefreshListener.refreshComplete();
    }
}
